import { BaseWindow } from "../../../../component/window/BaseWindow";
import { ChatGuildHintIcon } from "./ChatGuildHintIcon";
import { WindowEvent } from "../../../../component/window/WindowEvent";
import { ResManager } from "../../../../common/ResManager";
import { GTextFormat } from "../../../../common/GTextFormat";
import { FontUtil } from "../../../../common/font/FontUtil";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { KeyBoardManager } from "../../../../../com/gengine/keyBoard/KeyBoardManager";
import { GameMapUtil } from "../../../scene/map/GameMapUtil";
import { Cache } from "../../../cache/Cache";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { CheckBoxWin } from "../../common/alertwins/CheckBoxWin";
import { PulseSharedObject } from "../../player/PulseSharedObject";
import { Language } from "../../../../../extend/language/Language";
import { MsgManager } from "../../../manager/MsgManager";
import { RolePlayer } from "../../../scene/player/entity/RolePlayer";
import { ChatShowPoint } from "../data/ChatShowPoint";
import { MapFileUtil } from "../../../scene/map/MapFileUtil";
import { ChatArea } from "../ChatArea";
import { PlayerMiniInfo } from "../../../model/PlayerMiniInfo";
import { ObjectPool } from "../../../../../com/gengine/utils/pools/ObjectPool";
import { ChatItemData } from "../chatViewData/ChatItemData";
import { ChatMessageWorking } from "../chatViewData/ChatMessageWorking";
import { FaceAuthority } from "../data/FaceAuthority";
import { ChatCellData } from "../chatViewData/ChatCellData";
import { ChatItem } from "../chatPanel/ChatItem";
import { SGuildPlayer } from "../../../../../Message/Game/SGuildPlayer";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { Global } from "../../../../../com/gengine/global/Global";
import { ObjCreate } from "../../common/ObjCreate";
import { ChatPrivatePanel } from "../chatPrivate/ChatPrivatePanel";
import { GScrollPane } from "../../../../../com/mui/controls/GScrollPane";
import { ScrollPolicy } from "../../../../../fl/controls/ScrollPolicy";
import { GBox } from "../../../../../com/mui/containers/GBox";
import { GBoxDirection } from "../../../../../com/mui/containers/globalVariable/GBoxDirection";
import { FacePanel } from "../selectPanel/FacePanel";
import { ColorSelector } from "../selectPanel/ColorSelector";
import { TimeButton } from "../../common/button/TimeButton";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { TilePanel } from "../../equipment/TilePanel";
import { ChatGuildMemberCellRenderer } from "./ChatGuildMemberCellRenderer";
import { KeyCode } from "../../../../../com/gengine/keyBoard/KeyCode";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { GTextInput } from "../../../../../com/mui/controls/GTextInput";
import { GCheckBox } from "../../../../../com/mui/controls/GCheckBox";
import { Union } from "../../../../../ChatArea/Union";
import { _color } from "../../../../../this/_color";
type int = number;
//class ChatGuildWindow
    
    export  class ChatGuildWindow extends BaseWindow
    {
        constructor()
        {
            super();
            this.init();
            this.addListeners();
            this.requestData();
            this._icon = new ChatGuildHintIcon();
            this.addEventListener(WindowEvent.SHOW, this.onChatGuildShow);
            return;
        }

        /* internal  */onBtnFaceClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            ResManager.instance.LoadFace();
            if (this._facePanel.parent) 
            {
                this._facePanel.stopAllFace();
                this._facePanel.parent.removeChild(this._facePanel);
                this.removeStateCancelPanelEvent();
            }
            else 
            {
                this.addChild(this._facePanel);
                this._facePanel.startAllFace();
                this.addStageCancelPanelEvent();
            }
            return;
        }

        /* internal  */onColorSelector(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._colorSelector.parent) 
            {
                this._colorSelector.parent.removeChild(this._colorSelector);
                this.removeStateCancelPanelEvent();
            }
            else 
            {
                this.addChild(this._colorSelector);
                this.addStageCancelPanelEvent();
            }
            return;
        }

        /* internal  */selectFace(arg1: Object): void
        {
            if (this._facePanel.parent) 
            {
                this._facePanel.parent.removeChild(this._facePanel);
                this.removeStateCancelPanelEvent();
            }
            this._tiChat.appendText("/" + arg1.toString() + " ");
            this._tiChat.setFocus();
            this._tiChat.setSelection(this._tiChat.text.length, this._tiChat.text.length);
            return;
        }

        /* internal  */setInputTextFormat(): void
        {
            var loc1=new GTextFormat(FontUtil.songtiName, 12, this._color);
            loc1.leading = 3;
            this._tiChat.setStyle("textFormat", loc1);
            this._tiChat.setStyle("textPadding", 5);
            return;
        }

        /* internal  */onChatGuildShow(arg1: WindowEvent): void
        {
            this.requestData();
            return;
        }

        /* internal  */requestData(): void
        {
            var loc1=new Date();
            if (!this._lastRequestDate || loc1.time - this._lastRequestDate.time > this.MinRequestDataTime) 
            {
                this._lastRequestDate = loc1;
                Dispatcher.dispatchEvent(new DataEvent(EventName.GuildGetInfo, null));
                Dispatcher.dispatchEvent(new DataEvent(EventName.GuildGetPlayerList, null));
            }
            return;
        }

        /* internal  */onBtnLanguageClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._btnLanguage.styleName != "English") 
            {
                KeyBoardManager.instance.changeImeEnable(false);
                this._btnLanguage.styleName = "English";
            }
            else 
            {
                KeyBoardManager.instance.changeImeEnable(true);
                this._btnLanguage.styleName = "Chinese";
            }
            return;
        }

        /* internal  */selectColor(arg1: uint): void
        {
            if (this._colorSelector.parent) 
            {
                this._colorSelector.parent.removeChild(this._colorSelector);
                this.removeStateCancelPanelEvent();
            }
            this._color = arg1;
            this.setInputTextFormat();
            return;
        }

        /* internal  */onBtnDiceClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._btnSend.enabled) 
            {
                this._btnSend.trigger();
                Dispatcher.dispatchEvent(new DataEvent(EventName.ChatGuildDice, null));
            }
            return;
        }

        /* internal  */onBtnBrushClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            this._chatBox.reset();
            if (!this.isHide) 
            {
                this.timeOutUpdate();
            }
            return;
        }

        /* internal  */onBtnPointClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=false;
            if (GameMapUtil.isUniqueMap() || Cache.instance.guild.isInSelfManorMap()) 
            {
                Alert.alertWinRenderer = CheckBoxWin;
                loc1 = !PulseSharedObject.isTodayNotTips("SelfMapPoint");
                if (loc1) 
                {
                    Alert.alertWinRenderer = CheckBoxWin;
                    Alert.show(Language.getString(40683), null, Alert.OK | Alert.CANCEL, null, this.onChoosePoint);
                }
                else 
                {
                    this.addMapPoint();
                }
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(40684));
            }
            return;
        }

        /* internal  */onChoosePoint(arg1: int, arg2: Boolean): void
        {
            if (arg1 == Alert.OK) 
            {
                this.addMapPoint();
            }
            if (arg2) 
            {
                PulseSharedObject.save("SelfMapPoint", true);
            }
            return;
        }

        /* internal  */addMapPoint(): void
        {
            var loc1=GameMapUtil.getTilePoint(RolePlayer.instance.x, RolePlayer.instance.y);
            var loc2=new ChatShowPoint(MapFileUtil.mapID, loc1.x, loc1.y);
            this._tiChat.appendText(loc2.mapNameString());
            this._tiChat.setSelection(this._tiChat.text.length, this._tiChat.text.length);
            Dispatcher.dispatchEvent(new DataEvent(EventName.ChatShowPoint, loc2));
            return;
        }

        /* internal  */onBtnSendClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.sendMsg();
            return;
        }

        /* internal  */sendMsg(): void
        {
            var sentText: string;

            var loc1;
            sentText = this._tiChat.text;
            if (sentText && this._btnSend.enabled) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ChatSend, {"content": sentText, "area": Union, "color": _color}));
                this._btnSend.trigger();
                with ({}) 
                {
                };
                
#error('pushwith') {}
                .flash.utils.setTimeout(
#error('pushwith') {}
                .a = (): void
                {
                    _tiChat.text = "";
                    return;
                }, 10)
            }
            return;
        }

        public addChatItem(arg1: PlayerMiniInfo, arg2: Date, arg3: string, arg4: Array<any>, arg5: Array<any>, arg6: int=0, arg7: Boolean=false): void
        {
            var loc1;
            (loc1 = ObjectPool.getObject(ChatItemData)).type = ChatMessageWorking.getChatTypeByAreaName(ChatArea.Union);
            var loc2=FaceAuthority.getPlayerAuthority(arg1);
            var loc3=new Vector.<ChatCellData>();
            if (arg6 == 0) 
            {
                arg6 = 16645629;
            }
            if (arg7) 
            {
                loc3 = loc3.concat(ChatMessageWorking.getDiceCellData(arg3, arg6));
            }
            else 
            {
                loc3 = loc3.concat(ChatMessageWorking.getCellDatas(arg3, arg4, arg5, arg6, 12, loc2));
            }
            loc1.cellVector = loc3;
            var loc4;
            (loc4 = ObjectPool.getObject(ChatItem)).init(loc1.getContentElement(), 280, 22);
            this._chatBox.addChatItem(loc4, arg1.playerName, arg2);
            loc1.dispose();
            if (!this.isHide) 
            {
                this.timeOutUpdate();
            }
            return;
        }

        public updateNotice(arg1: string): void
        {
            this._tfNotice.htmlText = "<textFormat leading=\'5\'><font color=\'#FFFFFF\'>" + arg1 + "</font></textFormat>";
            return;
        }

        public updateMemberList(arg1: Array<any>): void
        {
            var loc4=null;
            var loc1=0;
            arg1.sort(this.compareMember);
            var loc2=0;
            while (loc2 < arg1.length) 
            {
                if ((loc4 = arg1[loc2] as SGuildPlayer).miniPlayer.online) 
                {
                    ++loc1;
                }
                ++loc2;
            }
            this._tfListTitle.text = Language.getString(40618) + "(" + loc1 + "/" + arg1.length + ")";
            var loc3=new DataProvider(arg1);
            this._memberTileList.dataProvider = loc3;
            this._memberTileList.drawNow();
            this._memberTileList.validateNow();
            return;
        }

        /* internal  */compareMember(arg1: SGuildPlayer, arg2: SGuildPlayer): int
        {
            if (arg1.miniPlayer.online > arg2.miniPlayer.online) 
            {
                return -1;
            }
            if (arg1.miniPlayer.online < arg2.miniPlayer.online) 
            {
                return 1;
            }
            if (arg1.position > arg2.position) 
            {
                return -1;
            }
            if (arg1.position < arg2.position) 
            {
                return 1;
            }
            if (arg1.miniPlayer.VIP > arg2.miniPlayer.VIP) 
            {
                return -1;
            }
            if (arg1.miniPlayer.VIP < arg2.miniPlayer.VIP) 
            {
                return 1;
            }
            return 0;
        }

        public ChatGuildWindow.get icon(): ChatGuildHintIcon
        {
            return this._icon;
        }

        public set isShowWindow(arg1: Boolean)
        {
            this._cbShowIcon.selected = !arg1;
            return;
        }

        public clearAndHide(): void
        {
            this._chatBox.reset();
            this.hide();
            return;
        }

        public /* override */ set visible(arg1: Boolean)
        {
            super.visible = arg1;
            if (arg1) 
            {
                this.timeOutUpdate();
                this.requestData();
            }
            return;
        }

        /* internal  */onAddToStage(arg1: Object /* flash.events.Event */): void
        {
            this.reSetScrollPanePosition();
            return;
        }

        /* internal  */removeStateCancelPanelEvent(): void
        {
            if (!(this._colorSelector.parent || this._facePanel.parent)) 
            {
                Global.stage.removeEventListener(flash.events.MouseEvent.CLICK, this.canselPanelsHandler);
            }
            return;
        }

        /* internal  */timeOutUpdate(): void
        {
            flash.utils.setTimeout(this.reSetScrollPanePosition, 100);
            return;
        }

        public reSetScrollPanePosition(): void
        {
            if (this._scrollPane && Global.stage.contains(this._scrollPane)) 
            {
                this._chatBox.validateNow();
                this._scrollPane.validateNow();
                this._scrollPane.update();
                this._scrollPane.verticalScrollPosition = this._scrollPane.maxVerticalScrollPosition;
            }
            return;
        }

        public static get instance(): ChatGuildWindow
        {
            if (!ChatGuildWindow._instance) 
            {
                ChatGuildWindow._instance = new ChatGuildWindow();
            }
            return ChatGuildWindow._instance;
        }

        protected /* override */ createChildren(): void
        {
            super.createChildren();
            this._btnMinimize1 = ObjCreate.createGButton("", 450, 8, 19, 19, this, "MinimizeButton");
            this._areaChat = new Object /* flash.display.Sprite */();
            this._areaChat.x = 19;
            this._areaChat.y = 40;
            this._areaChat.mouseEnabled = false;
            this.addChild(this._areaChat);
            var loc1=ObjCreate.createBg(0, 0, 323, 238, this._areaChat);
            this._chatBox = new ChatPrivatePanel();
            this._chatBox.width = 305;
            this._chatBox.height = 238;
            this._areaChat.addChild(this._chatBox);
            this._scrollPane = new GScrollPane();
            this._scrollPane.width = 322;
            this._scrollPane.height = 235;
            this._scrollPane.y = 3;
            this._scrollPane.styleName = "GScrollPane";
            this._scrollPane.verticalScrollPolicy = ScrollPolicy.AUTO;
            this._scrollPane.horizontalScrollPolicy = ScrollPolicy.OFF;
            this._scrollPane.source = this._chatBox;
            this._areaChat.addChild(this._scrollPane);
            this._scrollPane.update();
            this._btnFace = ObjCreate.createGButton("", 25, 283, 18, 17, this, "ExpressionChatButton");
            this._areaFuns = new GBox();
            this._areaFuns.x = 46;
            this._areaFuns.y = 280;
            this._areaFuns.direction = GBoxDirection.HORIZONTAL;
            this.addChild(this._areaFuns);
            this._btnColorSelector = ObjCreate.createGButton("", 0, 0, 25, 23, this._areaFuns, "ColorSelector");
            this._btnLanguage = ObjCreate.createGButton("", 0, 0, 25, 23, null, "English");
            this._btnDice = ObjCreate.createGButton("", 0, 0, 25, 23, this._areaFuns, "Dice");
            this._btnBrush = ObjCreate.createGButton("", 0, 0, 25, 23, this._areaFuns, "Brush");
            var loc2=new Object /* flash.display.Sprite */();
            this._btnPoint = ObjCreate.createGButton("", 0, 3, 18, 17, loc2, "PointBtn");
            this._areaFuns.addChild(loc2);
            this._btnFace.toolTipData = "<font color=\'#ffffff\'>" + Language.getString(40609) + "</font>";
            this._btnColorSelector.toolTipData = "<font color=\'#ffffff\'>" + Language.getString(40610) + "</font>";
            this._btnLanguage.toolTipData = "<font color=\'#ffffff\'>" + Language.getString(40611) + "</font>";
            this._btnDice.toolTipData = "<font color=\'#ffffff\'>" + Language.getString(40612) + "</font>";
            this._btnBrush.toolTipData = "<font color=\'#ffffff\'>" + Language.getString(40613) + "</font>";
            this._btnPoint.toolTipData = Language.getString(40687);
            this._facePanel = new FacePanel();
            this._facePanel.x = 29;
            this._facePanel.y = 97;
            this._colorSelector = new ColorSelector();
            this._colorSelector.x = 40;
            this._colorSelector.y = 160;
            this._areaInput = new Object /* flash.display.Sprite */();
            this._areaInput.x = 19;
            this._areaInput.y = 305;
            this._areaInput.mouseEnabled = false;
            this.addChild(this._areaInput);
            this._tiChat = ObjCreate.createGTextInput(0, 0, 323, 100, this._areaInput);
            this._tiChat.maxChars = 50;
            this._tiChat.mouseFocusEnabled = false;
            this._tiChat.textField.wordWrap = true;
            this.setInputTextFormat();
            this._cbShowIcon = ObjCreate.createCheckBox(Language.getString(40614), 15, 101, 140, 28, this._areaInput);
            this._btnMiniSize2 = ObjCreate.createGButton(Language.getString(40615), 178, 105, 70, 22, this._areaInput);
            this._btnSend = new TimeButton(3, false);
            this._btnSend.width = 70;
            this._btnSend.height = 22;
            this._btnSend.styleName = "Button";
            this._btnSend.x = 253;
            this._btnSend.label = Language.getString(40616);
            this._btnSend.y = 105;
            this._btnSend.textField.filters = [FilterConst.nameGlowFilter];
            this._areaInput.addChild(this._btnSend);
            this._areaRight = new Object /* flash.display.Sprite */();
            this._areaRight.x = 345;
            this._areaRight.y = 42;
            this._areaRight.mouseEnabled = false;
            this.addChild(this._areaRight);
            var loc3=new GTextFormat(FontUtil.songtiName, 14, 15788607);
            loc3.align = flash.text.TextFormatAlign.CENTER;
            ObjCreate.createBg(0, 0, 140, 388, this._areaRight);
            ObjCreate.createBg(-1, -3, 144, 35, this._areaRight, "RbListHeader");
            ObjCreate.createBg(-1, 144, 144, 35, this._areaRight, "RbListHeader");
            ObjCreate.createTextField(Language.getString(40617), 0, 5, 140, 20, this._areaRight, loc3);
            this._tfListTitle = ObjCreate.createTextField(Language.getString(40618) + "(0/0)", 0, 152, 140, 20, this._areaRight, loc3);
            this._tfNotice = ObjCreate.createTextField("", 5, 35, 135, 100, this._areaRight);
            this._tfNotice.multiline = true;
            this._tfNotice.wordWrap = true;
            this._memberTileList = new TilePanel();
            this._memberTileList.setSize(136, 209);
            this._memberTileList.x = 3;
            this._memberTileList.rowHeight = 21;
            this._memberTileList.columnWidth = 120;
            this._memberTileList.verticalGap = 3;
            this._memberTileList.setStyle("cellRenderer", ChatGuildMemberCellRenderer);
            this._memberTileList.y = 177;
            this._areaRight.addChild(this._memberTileList);
            return;
        }

        /* internal  */init(): void
        {
            this.setSize(503, 444);
            this.titleHeight = 26;
            title = Language.getString(40619);
            return;
        }

        /* internal  */addListeners(): void
        {
            this._btnMinimize1.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnMinimizeClick);
            this._btnMiniSize2.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnMinimizeClick);
            this._btnSend.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnSendClick);
            this._btnFace.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnFaceClick);
            this._btnColorSelector.addEventListener(flash.events.MouseEvent.CLICK, this.onColorSelector);
            this._btnLanguage.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnLanguageClick);
            this._btnDice.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnDiceClick);
            this._btnBrush.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnBrushClick);
            this._btnPoint.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnPointClick);
            this._tiChat.addEventListener(flash.events.KeyboardEvent.KEY_DOWN, this.keyHandler);
            this._facePanel.addCall(this.selectFace);
            this._colorSelector.addCall(this.selectColor);
            Global.stage.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddToStage);
            return;
        }

        /* internal  */addStageCancelPanelEvent(): void
        {
            Global.stage.addEventListener(flash.events.MouseEvent.CLICK, this.canselPanelsHandler);
            return;
        }

        public ChatGuildWindow.get isShowWindow(): Boolean
        {
            return !this._cbShowIcon.selected;
        }

        /* internal  */canselPanelsHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.target as Object /* flash.display.DisplayObject */;
            if (!(loc1 == this._btnColorSelector) && !(loc1 == this._colorSelector) && (!loc1.parent || !(loc1.parent == this._colorSelector))) 
            {
                if (this._colorSelector.parent) 
                {
                    this._colorSelector.parent.removeChild(this._colorSelector);
                }
            }
            if (!(loc1 == this._btnFace) && !(loc1 == this._facePanel) && !this._facePanel.contains(loc1)) 
            {
                if (this._facePanel.parent) 
                {
                    this._facePanel.parent.removeChild(this._facePanel);
                    this._facePanel.stopAllFace();
                }
            }
            return;
        }

        /* internal  */keyHandler(arg1: Object /* flash.events.KeyboardEvent */): void
        {
            if (arg1.keyCode == KeyCode.ENTER) 
            {
                this.sendMsg();
            }
            return;
        }

        /* internal  */onBtnMinimizeClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.visible = false;
            return;
        }

        /* internal */ /* const */MinRequestDataTime: int=60000;

        /* internal */ /* var */_icon: ChatGuildHintIcon;

        /* internal */ /* var */_playerInfo: PlayerMiniInfo;

        /* internal */ /* var */_lastRequestDate: Date;

        /* internal */ /* var */_btnMinimize1: GButton;

        /* internal */ /* var */_areaFuns: GBox;

        /* internal */ /* var */_areaInput: Object /* flash.display.Sprite */;

        /* internal */ /* var */_areaRight: Object /* flash.display.Sprite */;

        /* internal */ /* var */_scrollPane: GScrollPane;

        /* internal */ /* var */_chatBox: ChatPrivatePanel;

        /* internal */ /* var */_btnFace: GButton;

        /* internal */ /* var */_btnColorSelector: GButton;

        /* internal */ /* var */_btnLanguage: GButton;

        /* internal */ /* var */_btnDice: GButton;

        /* internal */ /* var */_btnBrush: GButton;

        /* internal */ /* var */_btnPoint: GButton;

        /* internal */ /* var */_facePanel: FacePanel;

        /* internal */ /* var */_colorSelector: ColorSelector;

        /* internal */ /* var */_tiChat: GTextInput;

        /* internal */ /* var */_cbShowIcon: GCheckBox;

        /* internal */ /* var */_btnMiniSize2: GButton;

        /* internal */ /* var */_tfListTitle: Object /* flash.text.TextField */;

        /* internal */ /* var */_tfNotice: Object /* flash.text.TextField */;

        /* internal */ /* var */_memberTileList: TilePanel;

        /* internal */ /* var */_areaChat: Object /* flash.display.Sprite */;

        /* internal */ /* var */_color: uint=16645629;

        /* internal */ /* var */_btnSend: TimeButton;

        /* internal */ static /* var */_instance: ChatGuildWindow;

        /* internal */ /* var */_isShowWindow: Boolean;
    }
